package code;

import sun.plugin.com.event.COMEventHandler;

/**
 * @author noob
 * @version 1.0
 * @date 2021/3/19 11:10
 */
public class BubbleSort {
    public BubbleSort() {
    }

    public static <E extends Comparable<E>> void sort(E[] data){

        for (int i = 0; i < data.length - 1; i++) { //只需要进行n-1论循环
            for (int j = 0; j < data.length  - i -1; j++) {
                if(data[j].compareTo(data[j+1]) > 0){
                    exchange(data,j,j+1);
                }


            }

        }

    }

    private static <E extends Comparable<E>> void exchange(E[] data, int i, int j) {
        E t = data[i];
        data[i] = data[j];
        data[j] = t;

    }


    public static void main(String[] args) {
        Integer[] data2 = {6,4,2,3,2,1,5,5,};
        BubbleSort.sort(data2);
        for (int i = 0; i < data2.length; i++) {
            System.out.println(data2[i]);
        }
    }

}
